import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class MathProblemGenerator {

    public static List<MathProblem> generateProblems(int count) {
        List<MathProblem> problems = new ArrayList<>();
        Random random = new Random();

        for (int i = 0; i < count; i++) {
            boolean isAddition = random.nextBoolean();
            String problem;
            int answer;

            if (isAddition) {
                int a = random.nextInt(99) + 1; // 1-99
                int b = random.nextInt(100 - a) + 1; // 确保和不超过100
                problem = a + " + " + b + " = ";
                answer = a + b;
            } else {
                int a = random.nextInt(99) + 1; // 1-99
                int b = random.nextInt(a) + 1; // 确保被减数不小于减数
                problem = a + " - " + b + " = ";
                answer = a - b;
            }

            problems.add(new MathProblem(problem, answer));
        }

        return problems;
    }

    public static void saveToCSV(List<MathProblem> problems, String filename) {
        try (PrintWriter writer = new PrintWriter(new FileWriter(filename))) {
            writer.println("Problem,Answer");
            for (MathProblem problem : problems) {
                writer.println(problem.getProblem() + "," + problem.getAnswer());
            }
            System.out.println("习题已保存到: " + filename);
        } catch (IOException e) {
            System.err.println("保存文件时出错: " + e.getMessage());
        }
    }

    public static void main(String[] args) {
        List<MathProblem> problems = generateProblems(50);
        saveToCSV(problems, "math_problems.csv");
    }
}